// ActionScript file
import EVENTS.GridActionClicked;

import flash.system.System;
import flash.utils.Dictionary;

import mx.collections.HierarchicalData;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.utils.StringUtil;

import org.flexlancer.Logger.Logger;
import org.flexlancer.databases.AdditionalDishes;
import org.flexlancer.databases.AppUsers;
import org.flexlancer.databases.Areas;
import org.flexlancer.databases.DishCategories;
import org.flexlancer.databases.Dishes;
import org.flexlancer.databases.DropBillReasons;
import org.flexlancer.databases.Person;
import org.flexlancer.databases.PersonType;

private function OnCC():void{
	try{
		this.maximize();
		this.txtUserID.setFocus();

		this.LoadData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private var NextMenuCode:Number = -2;

private function LoadData():void{
	try{
		this.dpDeliveryAreas = Areas.List();
		this.dpAppUsers = AppUsers.List();
		this.dpDropReasons = DropBillReasons.List();
		this.dpAdditionalItems =  AdditionalDishes.List();
		this.dpMenuHeads = DishCategories.List();
		this.dpWaitersRiders = Person.List();
		this.dpPersonType = PersonType.List();
		
		var dictDishes:Dictionary = new Dictionary;
		var dishes_tree:Array = DishCategories.List();
		
		for each ( var singleObj:Object in Dishes.List() ){
			if ( dictDishes[singleObj.itemcat] == undefined ){
				dictDishes[singleObj.itemcat] = new Array;
			}
			
			var arr:Array = dictDishes[singleObj.itemcat] as Array;
			arr.push(singleObj);
			if ( singleObj.itemcode > this.NextMenuCode ){
				this.NextMenuCode = singleObj.itemcode;
			}
		}

		for each ( var singleHead:Object in this.dpMenuHeads ){
			if ( dictDishes[singleHead.catid] == undefined ){
				singleHead.itemscount = 0;
			}else{
				singleHead.itemscount = dictDishes[singleHead.catid].length;
			}
		}
		for each ( var singleCat:Object in dishes_tree ){
			if ( dictDishes[singleCat.catid] == undefined ){
				singleCat.children = new Array;
			}else{
				singleCat.children = dictDishes[singleCat.catid];
			}
			singleCat.itemname = singleCat.catname;
		}
		
		for each ( var singlePerson:Object in this.dpWaitersRiders ){
			for each ( var singleType:Object in this.dpPersonType ){
				if ( singlePerson.typeid == singleType.persontypeid ){
					singlePerson.typename = singleType.typename;
					break;
				}
			}
		}
		this.dpMenuItems = new HierarchicalData(dishes_tree);
	}catch(err:Error){
		Logger.LogE(err);
	}
}
/*
===============================================================================================
===============================================================================================
                      LOGIN SCRIPTS
===============================================================================================
===============================================================================================
*/
[Bindable]
private var showManage:Boolean = false;
[Bindable]
private var welcome_lbl:String = "";

private function OnLogin():void{
	try{
		var arr:Array = AppUsers.List("userid LIKE '"+this.txtUserID.text+"' AND password LIKE '"+this.txtPassword.text+"'");
		if ( arr.length == 0 ){
			Alert.show("Invalid userid/password combination","Login Error",4,this,LoginFailed);
		}else if ( arr[0].allowmanage == 0 ){
			Alert.show("You don't have access to management. Please contact to administrator.","You are blocked",4,this,LoginFailed);
		}else{
			this.showManage = arr[0].allowmanage == 1 ? true : false;
			this.currentState = "Management";
			this.welcome_lbl = "Welcome Mr."+arr[0].userid;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function LoginFailed(evt:CloseEvent):void{
	this.txtUserID.setFocus();
}

/*
===============================================================================================
===============================================================================================
                      DELIVERY AREA SCRIPTS
===============================================================================================
===============================================================================================
*/
[Bindable]
private var dpDeliveryAreas:Array;

private var delete_item:Object;

private function DeliveryGrid_OnCC():void{
	try{
		if ( !this.dgDeliveryAreas.hasEventListener(GridActionClicked.GRID_ITEM_DELETE) )
			this.dgDeliveryAreas.addEventListener(GridActionClicked.GRID_ITEM_DELETE,DeliveryDeleted);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function DeliveryGrid_OnChange():void{
	try{
		if ( this.dgDeliveryAreas.selectedItem != null ){
			this.txtAreaName.text = this.dgDeliveryAreas.selectedItem.areaname;
			this.txtCharges.text = this.dgDeliveryAreas.selectedItem.charges;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function DeliveryDeleted(evt:GridActionClicked):void{
	try{
		this.delete_item = evt.Data;
		Alert.show("Are you sure to delete \""+this.delete_item.areaname+"\" ?","Delete Confirmation",Alert.YES|Alert.NO,this,this.OnDeliveryDeleteConfirmation);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnDeliveryDeleteConfirmation(evt:CloseEvent):void{
	try{
		if ( evt.detail == Alert.YES ){
			Areas.Delete("areaname LIKE '"+this.delete_item.areaname+"'");
			this.LoadData();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAddArea():void{
	try{
		this.txtAreaName.text = StringUtil.trim(this.txtAreaName.text);
		if ( this.txtAreaName.text != "" ){
			var found:Boolean = false;
			for each ( var singleA:Object in this.dpDeliveryAreas ){
				if ( singleA.areaname == this.txtAreaName.text.toUpperCase() ){
					found = true;
					break;
				}
			}
			if ( !found ){
				Areas.Add(this.txtAreaName.text.toUpperCase(),Number(this.txtCharges.text));
				this.dpDeliveryAreas = Areas.List();
				this.txtAreaName.text = "";
				this.txtCharges.text = "";
			}else{
				Areas.Update(Number(this.txtCharges.text),"areaname LIKE '"+this.txtAreaName.text.toUpperCase()+"'");
				this.dpDeliveryAreas = Areas.List();
				this.txtAreaName.text = "";
				this.txtCharges.text = "";
			}
			this.LoadData();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnResetArea():void{
	try{
		this.txtAreaName.text = "";
	}catch(err:Error){
		Logger.LogE(err);
	}
}
/*
===============================================================================================
===============================================================================================
                      APPUSER SCRIPTS
===============================================================================================
===============================================================================================
*/
[Bindable]
private var dpAppUsers:Array;

private function OnSaveAppUser():void{
	try{
		this.txtSaveUserID.text = StringUtil.trim(this.txtSaveUserID.text);
		this.txtSavePassword.text = StringUtil.trim(this.txtSavePassword.text);
		
		if ( this.txtSaveUserID.text == "" || this.txtSavePassword.text == "" ){
			Alert.show("User ID and/or Password required");
			return; 
		}
		
		var found:Boolean = false;
		
		for each ( var singleU:Object in this.dpAppUsers ){
			if ( singleU.userid.toUpperCase() == this.txtSaveUserID.text.toUpperCase() ){
				Alert.show("User ID already exists. Are you sure to update the existing user with current one?","Warning",Alert.YES | Alert.NO,this,OnUpdateUserConfirmation);
				return;
			}
		}
		
		var ar:int = this.chkAllowReports.selected ? 1 : 0;
		var ap:int = this.chkAllowPOS.selected ? 1 : 0;
		var am:int = this.chkAllowManage.selected ? 1 : 0;
		
		AppUsers.Add(this.txtSaveUserID.text,this.txtSavePassword.text,ar,ap,am);
		this.LoadData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnUpdateUserConfirmation(evt:CloseEvent):void{
	try{
		if ( evt.detail == Alert.YES ){
			var ar:int = this.chkAllowReports.selected ? 1 : 0;
			var ap:int = this.chkAllowPOS.selected ? 1 : 0;
			var am:int = this.chkAllowManage.selected ? 1 : 0;
			
			AppUsers.Update(this.txtSaveUserID.text,this.txtSavePassword.text,ar,ap,am);
			this.LoadData();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function DgAppUsers_OnChange():void{
	try{
		if ( this.dgAppUsers.selectedItem != null ){
			this.txtSaveUserID.text = this.dgAppUsers.selectedItem.userid;
			this.txtSavePassword.text = this.dgAppUsers.selectedItem.password;
			this.chkAllowReports.selected = this.dgAppUsers.selectedItem.allowreports == 1 ? true : false;
			this.chkAllowPOS.selected = this.dgAppUsers.selectedItem.allowpos == 1 ? true : false;
			this.chkAllowManage.selected = this.dgAppUsers.selectedItem.allowmanage == 1 ? true : false;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function AppUserDeleted(evt:GridActionClicked):void{
	try{
		this.delete_item = evt.Data;
		Alert.show("Are you sure to delete \""+this.delete_item.userid+"\" ?","Delete Confirmation",Alert.YES|Alert.NO,this,this.OnAppUserDeleteConfirmation);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAppUserDeleteConfirmation(evt:CloseEvent):void{
	try{
		if ( evt.detail == Alert.YES ){
			AppUsers.Delete("userid LIKE '"+this.delete_item.userid+"'");
			this.LoadData();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function DgAppUsers_OnCC():void{
	try{
		if ( !this.dgAppUsers.hasEventListener(GridActionClicked.GRID_ITEM_DELETE) )
			this.dgAppUsers.addEventListener(GridActionClicked.GRID_ITEM_DELETE,AppUserDeleted);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

/*
===============================================================================================
===============================================================================================
                      DROP REASONS SCRIPTS
===============================================================================================
===============================================================================================
*/

[Bindable]
private var dpDropReasons:Array;

private function OnSaveDropReason():void{
	try{
		this.txtSaveReason.text = StringUtil.trim(this.txtSaveReason.text);
		
		if ( this.txtSaveReason.text == "" ){
			return; 
		}
		
		var found:Boolean = false;
		
		for each ( var singleR:Object in this.dpDropReasons){
			if ( singleR.reason.toUpperCase() == this.txtSaveReason.text.toUpperCase() ){
				Alert.show("Reason already exists. Are you sure to update the existing Reason with current one?","Warning",Alert.YES | Alert.NO,this,OnUpdateReasonConfirmation);
				return;
			}
		}
		
		var mc:int = this.chkReasonMarkCredit.selected ? 1 : 0;
		
		DropBillReasons.Add(this.txtSaveReason.text.toUpperCase(),mc);
		this.LoadData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnUpdateReasonConfirmation(evt:CloseEvent):void{
	try{
		if ( evt.detail == Alert.YES ){
			var mc:int = this.chkReasonMarkCredit.selected ? 1 : 0;
			
			DropBillReasons.Update(this.txtSaveReason.text.toUpperCase(),mc);
			this.LoadData();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function DgDropReason_OnChange():void{
	try{
		if ( this.dgDropReasons.selectedItem != null ){
			this.txtSaveReason.text = this.dgDropReasons.selectedItem.reason;
			this.chkReasonMarkCredit.selected = this.dgDropReasons.selectedItem.iscredit == 1 ? true : false;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function DropReasonDeleted(evt:GridActionClicked):void{
	try{
		this.delete_item = evt.Data;
		Alert.show("Are you sure to delete \""+this.delete_item.reason+"\" ?","Delete Confirmation",Alert.YES|Alert.NO,this,this.OnDropReasonDeleteConfirmation);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnDropReasonDeleteConfirmation(evt:CloseEvent):void{
	try{
		if ( evt.detail == Alert.YES ){
			DropBillReasons.Delete("reason LIKE '"+this.delete_item.reason+"'");
			this.dpDropReasons = DropBillReasons.List();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function DgDropReason_OnCC():void{
	try{
		if ( !this.dgDropReasons.hasEventListener(GridActionClicked.GRID_ITEM_DELETE) )
			this.dgDropReasons.addEventListener(GridActionClicked.GRID_ITEM_DELETE,DropReasonDeleted);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

/*
===============================================================================================
===============================================================================================
                      MENU HEADS SCRIPTS
===============================================================================================
===============================================================================================
*/
[Bindable]
private var dpMenuHeads:Array;

private function OnLoadMenuHeads():void{
	try{
		if ( !this.dgMenuHeads.hasEventListener(GridActionClicked.GRID_ITEM_DELETE) )
			this.dgMenuHeads.addEventListener(GridActionClicked.GRID_ITEM_DELETE,OnHeadsDeleted);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnHeadsDeleted(evt:GridActionClicked):void{
	try{
		if ( evt.Data.itemscount == 0 ){
			DishCategories.Delete("catid="+evt.Data.catid);
			this.LoadData();
		}else{
			Alert.show("Please delete all dishes before deleting head");
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnEnterMenuHeads():void{
	try{
		if ( StringUtil.trim(this.txtMenuHeads.text) != "" ){
			if ( this.dgMenuHeads.selectedItem == null ){
				DishCategories.Add(this.txtMenuHeads.text.toUpperCase());
			}else{
				DishCategories.Update(Number(this.dgMenuHeads.selectedItem.catid),this.txtMenuHeads.text.toUpperCase());
			}
			this.LoadData();
			this.txtMenuHeads.text = "";
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnMenuHeadChange():void{
	try{
		if ( this.dgMenuHeads.selectedItem != null ){
			this.txtMenuHeads.text = this.dgMenuHeads.selectedItem.catname;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnMenuHeadReset():void{
	try{
		this.txtMenuHeads.text = "";
	}catch(err:Error){
		Logger.LogE(err);
	}
}

/*
===============================================================================================
===============================================================================================
                      MENU ITEMS SCRIPTS
===============================================================================================
===============================================================================================
*/

[Bindable]
private var dpMenuItems:HierarchicalData;

private function OnLoadMenuItems():void{
	try{
		if ( !this.dgMenuHeads.hasEventListener(GridActionClicked.GRID_ITEM_DELETE) )
			this.dgMenuHeads.addEventListener(GridActionClicked.GRID_ITEM_DELETE,OnMenuItemsDeleted);

		
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnMenuItemsDeleted(evt:GridActionClicked):void{
	try{
		Dishes.Delete("dishid="+evt.Data.dishid);
		this.LoadData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnCopyMenu():void{
	try{
		var retValue:String = "Item Code, Category , Name, Dine In, Take Away, Delivery \r\n";
		
		for each ( var eachHead:Object in this.dpMenuItems.source ){
			for each ( var eachItem:Object in eachHead.children ){
				retValue += eachItem.itemcode + " , " + eachHead.catname + " , " + eachItem.itemname + " , " + eachItem.dinein + " , " + eachItem.takeaway + " , " + eachItem.delivery + "\r\n";
			}
		}
		System.setClipboard(retValue);
		Alert.show("Menu copied");
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnEnterMenuItems():void{
	try{
		if ( StringUtil.trim(this.txtMenuItemName.text) != ""
			&& StringUtil.trim(this.txtMenuItemCode.text) != ""
			&& StringUtil.trim(this.txtMenuItemDineIn.text) != ""
			&& StringUtil.trim(this.txtMenuItemTakeAway.text) != ""
			&& StringUtil.trim(this.txtMenuItemDelivery.text) != "" ){
			
			if ( this.adgMenuItems.selectedItem == null ){
				Alert.show("Please select the valid  head from below");
				return;
			}
			
			if ( this.adgMenuItems.selectedItem.children == undefined ){
				Dishes.Update(Number(this.adgMenuItems.selectedItem.dishid)
						, this.txtMenuItemName.text.toUpperCase()
						, Number(this.txtMenuItemCode.text)
						, Number(this.txtMenuItemDineIn.text)
						, Number(this.txtMenuItemTakeAway.text)
						, Number(this.txtMenuItemDelivery.text));
			}else{
				Dishes.Add( Number(this.txtMenuItemCode.text)
						, this.txtMenuItemName.text.toUpperCase()
						, Number(this.adgMenuItems.selectedItem.catid)
						, Number(this.txtMenuItemDineIn.text)
						, Number(this.txtMenuItemTakeAway.text)
						, Number(this.txtMenuItemDelivery.text));
			}

			this.LoadData();
			
			this.txtMenuItemName.text = "";
			this.txtMenuItemCode.text = "";
			this.txtMenuItemDineIn.text = "";
			this.txtMenuItemTakeAway.text = "";
			this.txtMenuItemDelivery.text = "";
		}else{
			Alert.show("All fields are required one.");
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnMenuItemChange():void{
	try{
		if ( this.adgMenuItems.selectedItem.children == undefined ){
			var objData:Object = this.adgMenuItems.selectedItem;
			this.txtMenuItemName.text = objData.itemname;
			this.txtMenuItemCode.text = objData.itemcode;
			this.txtMenuItemDineIn.text = objData.dinein;
			this.txtMenuItemTakeAway.text = objData.takeaway;
			this.txtMenuItemDelivery.text = objData.delivery;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnMenuItemReset():void{
	try{
		this.txtMenuItemName.text = "";
		this.txtMenuItemCode.text = "";
		this.txtMenuItemDineIn.text = "";
		this.txtMenuItemTakeAway.text = "";
		this.txtMenuItemDelivery.text = "";
		this.adgMenuItems.selectedItem = null;
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnMenuItemShowAll():void{
	try{
		this.adgMenuItems.expandAll();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function GetNextCode():void{
	try{
		this.txtMenuItemCode.text = String(this.NextMenuCode + 1);
	}catch(err:Error){
		
	}
}
/*
===============================================================================================
===============================================================================================
                      ADDITIONAL ITEMS SCRIPTS
===============================================================================================
===============================================================================================
*/

[Bindable]
private var dpAdditionalItems:Array;

private function OnLoadAdditionalItems():void{
	try{
		if ( !this.dgAdditionalItems.hasEventListener(GridActionClicked.GRID_ITEM_DELETE) )
			this.dgAdditionalItems.addEventListener(GridActionClicked.GRID_ITEM_DELETE,OnAdditionalItemsDeleted);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAdditionalItemsDeleted(evt:GridActionClicked):void{
	try{
		AdditionalDishes.Delete("dishid="+evt.Data.dishid);
		this.LoadData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnEnterAdditionalItems():void{
	try{
		if ( StringUtil.trim(this.txtAdditionalItemName.text) != ""
			&& StringUtil.trim(this.txtAdditionalDineIn.text) != ""
			&& StringUtil.trim(this.txtAdditionalTakeAway.text) != ""
			&& StringUtil.trim(this.txtAdditionalDelivery.text) != "" ){
			
			if ( this.dgAdditionalItems.selectedItem == null ){
				AdditionalDishes.Add(this.txtAdditionalItemName.text.toUpperCase()
									, Number(this.txtAdditionalDineIn.text)
									, Number(this.txtAdditionalTakeAway.text)
									, Number(this.txtAdditionalDelivery.text));
			}else{
				AdditionalDishes.Update( Number(this.dgAdditionalItems.selectedItem.dishid)
									, this.txtAdditionalItemName.text.toUpperCase()
									, Number(this.txtAdditionalDineIn.text)
									, Number(this.txtAdditionalTakeAway.text)
									, Number(this.txtAdditionalDelivery.text));
			}	

			this.LoadData();
			
			this.txtAdditionalItemName.text = "";
			this.txtAdditionalDineIn.text = "";
			this.txtAdditionalTakeAway.text = "";
			this.txtAdditionalDelivery.text = "";
		}else{
			Alert.show("All fields are required one.");
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAdditionalItemChange():void{
	try{
		if ( this.dgAdditionalItems.selectedItem.children == undefined ){
			var objData:Object = this.dgAdditionalItems.selectedItem;
			this.txtAdditionalItemName.text = objData.itemname;
			this.txtAdditionalDineIn.text = objData.dinein;
			this.txtAdditionalTakeAway.text = objData.takeaway;
			this.txtAdditionalDelivery.text = objData.delivery;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAdditionalItemReset():void{
	try{
		this.txtAdditionalItemName.text = "";
		this.txtAdditionalDineIn.text = "";
		this.txtAdditionalTakeAway.text = "";
		this.txtAdditionalDelivery.text = "";
		this.dgAdditionalItems.selectedItem = null;
	}catch(err:Error){
		Logger.LogE(err);
	}
}

/*
===============================================================================================
===============================================================================================
                      WAITERS AN RIDERS SCRIPTS
===============================================================================================
===============================================================================================
*/

[Bindable]
private var dpWaitersRiders:Array;
[Bindable]
private var dpPersonType:Array;

private function OnLoadWaiterRiders():void{
	try{
		if ( !this.dgWaiterRiders.hasEventListener(GridActionClicked.GRID_ITEM_DELETE) )
			this.dgWaiterRiders.addEventListener(GridActionClicked.GRID_ITEM_DELETE,OnWaiterRidersDeleted);
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnWaiterRidersDeleted(evt:GridActionClicked):void{
	try{
		Person.Delete("id="+evt.Data.id);
		this.LoadData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnEnterWaiterRiders():void{
	try{
		if ( StringUtil.trim(this.txtWaiterRiderName.text) != ""
			&& StringUtil.trim(this.txtWaiterRiderCNIC.text) != ""
			&& StringUtil.trim(this.txtWaiterRiderContactNumber.text) != ""
			&& StringUtil.trim(this.txtWaiterRiderAddress.text) != "" 
			&& this.cmbWaiterRiderType.selectedItem != null
			){
			var refid:Number = Number(this.cmbWaiterRiderReferBy.selectedItem != null ? this.cmbWaiterRiderReferBy.selectedItem.id : 0); 
			if ( this.dgWaiterRiders.selectedItem == null ){
				Person.Add(refid
							, this.txtWaiterRiderName.text.toUpperCase()
							, Number(this.cmbWaiterRiderType.selectedItem.persontypeid)
							, this.txtWaiterRiderCNIC.text
							, this.txtWaiterRiderContactNumber.text
							, this.txtWaiterRiderAddress.text);
			}else{
				Person.Update( Number(this.dgWaiterRiders.selectedItem.id)
							, refid
							, this.txtWaiterRiderName.text
							, Number(this.cmbWaiterRiderType.selectedItem.persontypeid)
							, this.txtWaiterRiderCNIC.text
							, this.txtWaiterRiderContactNumber.text
							, this.txtWaiterRiderAddress.text);
			}	

			this.LoadData();
			this.OnWaiterRidersReset();			
		}else{
			Alert.show("All fields are required one.");
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnWaiterRidersChange():void{
	try{
		if ( this.dgWaiterRiders.selectedItem.children == undefined ){
			var objData:Object = this.dgWaiterRiders.selectedItem;
			
			this.txtWaiterRiderName.text = objData.name;
			this.txtWaiterRiderCNIC.text = objData.cnic;
			this.txtWaiterRiderContactNumber.text = objData.phone;
			this.txtWaiterRiderAddress.text = objData.address;
			
			this.cmbWaiterRiderReferBy.selectedItem = null;
			for each ( var singlePerson:Object in this.dpWaitersRiders ){
				if ( singlePerson.id == objData.refid ){
					this.cmbWaiterRiderReferBy.selectedItem = singlePerson;
					break;
				}
			}
			
			this.cmbWaiterRiderType.selectedItem = null ;
			for each ( var singleType:Object in this.dpPersonType ){
				if ( singleType.persontypeid == objData.typeid ){
					this.cmbWaiterRiderType.selectedItem = singleType;
					break;
				}
			}
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnWaiterRidersReset():void{
	try{
		this.txtWaiterRiderAddress.text = "";
		this.txtWaiterRiderContactNumber.text="";
		this.txtWaiterRiderName.text = "";
		this.txtWaiterRiderCNIC.text = "";
		this.cmbWaiterRiderReferBy.selectedItem = null;
		this.cmbWaiterRiderType.selectedItem = null;

		this.dgWaiterRiders.selectedItem = null;
	}catch(err:Error){
		Logger.LogE(err);
	}
}

